﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Administration_Users_Detail : System.Web.UI.Page {
    private MembershipUser user;

    protected void Page_Load(object sender, EventArgs e) {
        if (string.IsNullOrEmpty(Request.QueryString["UserName"])) Response.Redirect("Default.aspx");
        this.user = Membership.GetUser(Request.QueryString["UserName"], false);

        if (this.user == null) {
            this.LiteralErrorMessage.Text = string.Format(this.LiteralErrorMessage.Text, "User not found");
            this.MultiViewPage.SetActiveView(this.ViewMessage);
        }

        this.LabelUserName.Text = this.user.UserName;
        this.IconHyperLinkDelete.NavigateUrl = string.Format(this.IconHyperLinkDelete.NavigateUrl, this.user.UserName);

        if (this.user.UserName.Equals(this.User.Identity.Name, StringComparison.OrdinalIgnoreCase) || this.user.UserName.Equals("administrator", StringComparison.OrdinalIgnoreCase)) {
            this.IconHyperLinkDelete.Visible = false;
            this.CheckBoxEnabled.Enabled = false;
        }


        if (!this.IsPostBack) {
            this.TextBoxEmail.Text = user.Email;
            this.TextBoxFullName.Text = user.Comment;
            this.CheckBoxEnabled.Checked = user.IsApproved;
        }

    }

    protected void ButtonSubmit_Click(object sender, EventArgs e) {
        if (!this.IsValid) return;

        this.user.Email = this.TextBoxEmail.Text;
        this.user.Comment = this.TextBoxFullName.Text;
        Membership.UpdateUser(this.user);

        if (!string.IsNullOrEmpty(this.TextBoxPassword.Text)) {
            string tempPassword = this.user.ResetPassword();
            this.user.ChangePassword(tempPassword, this.TextBoxPassword.Text);
        }

        Response.Redirect("Default.aspx");
    }

    protected void ButtonCancel_Click(object sender, EventArgs e) {
        Response.Redirect("Default.aspx");
    }
}
